package com.wuyan.web.base.service.view;

import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;

import com.wuyan.web.base.entity.PubAccount;
import com.wuyan.web.base.entity.PubUser;
import com.wuyan.web.base.entity.view.PubViewAccount;
import com.wuyan.web.base.form.PubAccountForm;
import com.wuyan.web.base.form.PubUserForm;
import com.wuyan.web.base.form.view.PubViewAccountForm;

import com.wuyan.web.base.helper.rep.RepPageData;

import com.wuyan.web.base.repo.PubUserRepoExtend;
import com.wuyan.web.base.service.PubAccountService;
import com.wuyan.web.base.service.PubUserService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * service for PubViewAccount generated by jpa-codegen
 * TODO
 *
 * @author wuyan
 * @date 2021-05-18 09:22:30
 */

@Slf4j
@Primary
@Service
public class PubViewAccountServiceExtend extends PubViewAccountService {
    @Autowired
    private PubAccountService pubAccountService;

    @Autowired
    private PubUserService pubUserService;

    @Autowired
    private PubUserRepoExtend pubUserRepoExtend;

    /**
     * 创建实体
     *
     * @param form 表单
     * @return 实体对象
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public PubViewAccount create(PubViewAccountForm form) throws Exception {
        PubAccountForm pubAccountForm = PubAccountForm.builder().build();
        BeanUtils.copyProperties(form, pubAccountForm);

        // 账户信息
        PubAccount pubAccount = pubAccountService.create(pubAccountForm);

        // 用户信息
        PubUser pubUser = pubUserRepoExtend.findFirstByUserId(pubAccount.getId());
        PubUserForm pubUserForm = PubUserForm.builder().build();
        BeanUtils.copyProperties(pubUser, pubUserForm);
        BeanUtils.copyProperties(form, pubUserForm);
        pubUserService.update(pubUserForm, pubUser.getId());

        // 返回数据
        Optional<PubViewAccount> pubViewAccountOptional = get(pubAccount.getId());
        return pubViewAccountOptional.orElse(null);
    }

    /**
     * 创建实体-批量
     * 根据 EntityConfig.ER_BATCH_INSERT_ON_CLEAR判断是否需要执行对应的清除操作
     *
     * @param forms 表单集合
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<PubViewAccount> creates(List<PubViewAccountForm> forms) {
        return forms.stream().map(t -> {
            try {
                return create(t);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
            return null;
        }).collect(Collectors.toList());
    }

    /**
     * 删除实体
     *
     * @param id 需要被删除的行ID组
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer delete(Integer[] id) {
        return pubAccountService.delete(id);
    }

    /**
     * 更新实体
     *
     * @param form 表单
     * @param id   实体id
     * @return 实体对象
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Optional<PubViewAccount> update(PubViewAccountForm form, Integer id) throws Exception {
        PubAccountForm pubAccountForm = PubAccountForm.builder().build();
        BeanUtils.copyProperties(form, pubAccountForm);

        // 账户信息
        pubAccountService.update(pubAccountForm, id);

        // 用户信息
        PubUser pubUser = pubUserRepoExtend.findFirstByUserId(id);
        PubUserForm pubUserForm = PubUserForm.builder().build();
        BeanUtils.copyProperties(pubUser, pubUserForm);
        BeanUtils.copyProperties(form, pubUserForm);
        pubUserService.update(pubUserForm, pubUser.getId());

        return get(id);
    }

    /**
     * 根据ID更新指定字段的值
     *
     * @param id    ID
     * @param field 字段名
     * @param value 字段值
     * @return long
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public long updateByField(Integer id, String field, String value) throws Exception {
        return pubAccountService.updateByField(id, field, value);
    }

    /**
     * 获取一个实体对象
     *
     * @param id 实体id
     * @return 实体对象
     */
    @Override
    public Optional<PubViewAccount> get(Integer id) {
        return super.get(id);
    }

    /**
     * 分页接口
     *
     * @param predicates      查询条件
     * @param orderSpecifiers 排序
     * @param isPage          是否分页
     * @param page            当前页
     * @param limit           每页大小
     * @return 分页结果
     */
    @Override
    public RepPageData<PubViewAccount> page(List<Predicate> predicates, List<OrderSpecifier<?>> orderSpecifiers, Boolean isPage, Integer page, Integer limit) {
        return super.page(predicates, orderSpecifiers, isPage, page, limit);
    }
}